package com.example.huixingzheng.activity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.huixingzheng.R;
import com.example.huixingzheng.util.DBHelper;

public class RegisterActivity extends AppCompatActivity {

    private DBHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        dbHelper = new DBHelper(this);

        Button register = findViewById(R.id.register_submit);
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 获取用户输入的账号、密码和账号类型
                EditText etAccount = findViewById(R.id.et_register_account);
                EditText etPassword = findViewById(R.id.et_register_password);
                EditText reEtPassword = findViewById(R.id.re_ed_register_password);

                String account = etAccount.getText().toString();
                String password = etPassword.getText().toString();
                String rePassword = reEtPassword.getText().toString();
                String type = "user";

                // 检查输入是否合法
                if (account.isEmpty() || password.isEmpty() || rePassword.isEmpty() ) {
                    Toast.makeText(RegisterActivity.this, "请填写完整信息", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (isAccountExists(account)) {
                    Toast.makeText(RegisterActivity.this, "该账号已存在", Toast.LENGTH_SHORT).show();
                    return;
                }

                // 将注册信息插入到数据库中
                if (registerAccount(account, password, rePassword, type)) {
                    Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    Toast.makeText(RegisterActivity.this, "注册失败，请稍后重试", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    // 检查账号是否已存在
    private boolean isAccountExists(String account) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery("SELECT * FROM LoginAccount WHERE account =?", new String[]{account});
        boolean exists = cursor.moveToFirst();
        cursor.close();
        db.close();
        return exists;
    }

    // 将注册信息插入到数据库中
    private boolean registerAccount(String account, String password, String rePassword, String type) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        if(!password.equals(rePassword)) {
            Toast.makeText(RegisterActivity.this, "两次密码输入不一致，请重新输入密码", Toast.LENGTH_SHORT).show();
        }
        try {
            db.execSQL("INSERT INTO LoginAccount (account, password, type) VALUES (?,?,?)", new Object[]{account, password, type});
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            db.close();
        }
    }
}